package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.lock.CharBookConfigReading;
import com.lineage.server.datatables.lock.CharBookReading;
import com.lineage.server.datatables.lock.ClanReading;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_MapTimerOut;
import com.lineage.server.serverpackets.S_OwnCharStatus;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.S_PacketBoxLoc;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1BookConfig;
import com.lineage.server.templates.L1BookMark;
import com.lineage.server.utils.L1SpawnUtil;
import com.lineage.server.utils.log.PlayerLogUtil;
import com.lineage.server.world.World;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Windows extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_Windows.class);
  
  public static void 玩家检举(String info) {
    PlayerLogUtil.writeLog("[玩家检举]", info);
  }
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      int targetId, itemobjid;
      String name;
      byte[] data;
      int changeCount, emblemStatus, mapIndex;
      L1Object temp;
      int selectdoor, mapid;
      L1BookConfig config;
      int i;
      L1Clan clan;
      int point, x;
      L1PcInstance[] onlineClanMember;
      int locx, y, length, locy, zone, j;
      L1PcInstance target;
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      int type = readC();
      switch (type) {
        case 0:
          targetId = readD();
          temp = World.get().findObject(targetId);
          if (temp instanceof L1PcInstance) {
            L1PcInstance targetPc = (L1PcInstance)temp;
            if (pc.getId() == targetPc.getId()) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(3130));
            } else if (pc.getLevel() < 10) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(3129));
            } else {
              Iterator<L1PcInstance> iterator = World.get().getAllPlayers().iterator();
              while (iterator.hasNext()) {
                Object visible = iterator.next();
                if (visible instanceof L1PcInstance) {
                  L1PcInstance GM = (L1PcInstance)visible;
                  if (!GM.isGm() || pc.getId() == GM.getId())
                    continue; 
                  GM.sendPackets((ServerBasePacket)new S_ServerMessage(
                        "\\fW玩家" + pc.getName() + " 申诉:(" + targetPc.getName() + ")使用外挂"));
                } 
              } 
              if (!pc.isGm()) {
                pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT感谢您申诉，为了游戏品质我们会严加监控"));
                pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT系统立即回报给予线上GM以及备份回报!!"));
                玩家检举("玩家:【 " + pc.getName() + " 】 " + "举报玩家" + ":【 " + targetPc.getName() + " 】" + 
                    "为外挂使用者，请注意，" + "检举时间:" + "(" + new Timestamp(System.currentTimeMillis()) + ")。");
              } 
              break;
            } 
            return;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(1023));
          break;
        case 6:
          itemobjid = readD();
          selectdoor = readD();
          if (pc.getInventory().getItem(itemobjid) == null)
            break; 
          switch (selectdoor) {
            case 0:
              if (pc.getInventory().consumeItem(47010, 1L))
                L1SpawnUtil.spawn(pc, 70932, 0, 7200); 
              break;
            case 1:
              if (pc.getInventory().consumeItem(47010, 1L))
                L1SpawnUtil.spawn(pc, 70937, 0, 7200); 
              break;
            case 2:
              if (pc.getInventory().consumeItem(47010, 1L))
                L1SpawnUtil.spawn(pc, 70934, 0, 7200); 
              break;
            case 3:
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("该副本尚未实装。"));
              break;
          } 
          return;
        case 11:
          name = readS();
          mapid = readH();
          x = readH();
          y = readH();
          zone = readD();
          target = World.get().getPlayer(name);
          if (target != null) {
            target.sendPackets((ServerBasePacket)new S_PacketBoxLoc(pc.getName(), mapid, x, y, zone));
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(1783, name));
            break;
          } 
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(1782));
          break;
        case 34:
          data = readBytes();
          config = CharBookConfigReading.get().get(pc.getId());
          if (config == null) {
            CharBookConfigReading.get().storeCharBookConfig(pc.getId(), data);
            break;
          } 
          CharBookConfigReading.get().updateCharBookConfig(pc.getId(), data);
          break;
          

        case 39:
          changeCount = readD();
          i = 0;
          while (i < changeCount) {
            int bookId = readD();
            L1BookMark bookm = CharBookReading.get().getBookMark(pc, bookId);
            if (bookm != null) {
              String changeName = readS();
              bookm.setName(changeName);
              CharBookReading.get().updateBookmarkName(bookm);
            } 
            i++;
          } 
          break;
        case 44:
          pc.setKillCount(0);
          pc.sendPackets((ServerBasePacket)new S_OwnCharStatus(pc));
          break;
        case 9:
          pc.sendPackets((ServerBasePacket)new S_MapTimerOut(pc));
          break;
        case 46:
          if (pc.getClanRank() != 4 && pc.getClanRank() != 10)
            return; 
          emblemStatus = readC();
          clan = pc.getClan();
          clan.setEmblemStatus(emblemStatus);
          ClanReading.get().updateClan(clan);
          length = (onlineClanMember = clan.getOnlineClanMember()).length;
          j = 0;
          while (j < length) {
            L1PcInstance member = onlineClanMember[j];
            member.sendPackets((ServerBasePacket)new S_PacketBox(173, emblemStatus));
            j++;
          } 
          break;
        case 48:
          mapIndex = readH();
          point = readH();
          locx = 0;
          locy = 0;
          if (mapIndex == 1) {
            if (point == 0) {
              locx = 34079 + (int)(Math.random() * 12.0D);
              locy = 33136 + (int)(Math.random() * 15.0D);
            } else if (point == 1) {
              locx = 33970 + (int)(Math.random() * 10.0D);
              locy = 33243 + (int)(Math.random() * 14.0D);
            } else if (point == 2) {
              locx = 33925 + (int)(Math.random() * 14.0D);
              locy = 33351 + (int)(Math.random() * 9.0D);
            } 
          } else if (mapIndex == 2) {
            if (point == 0) {
              locx = 32615 + (int)(Math.random() * 11.0D);
              locy = 32719 + (int)(Math.random() * 7.0D);
            } else if (point == 1) {
              locx = 32621 + (int)(Math.random() * 9.0D);
              locy = 32788 + (int)(Math.random() * 13.0D);
            } 
          } else if (mapIndex == 3) {
            if (point == 0) {
              locx = 33501 + (int)(Math.random() * 11.0D);
              locy = 32765 + (int)(Math.random() * 9.0D);
            } else if (point == 1) {
              locx = 33440 + (int)(Math.random() * 11.0D);
              locy = 32784 + (int)(Math.random() * 11.0D);
            } 
          } else if (mapIndex == 4) {
            int[][] loc = { { 32838, 32886 }, { 32800, 32874 }, { 32755, 32899 }, { 32741, 32938 }, { 32740, 32964 }, { 32801, 32982 }, { 32845, 32986 }, { 32852, 32932 }, { 32799, 32927 } };
            locx = loc[point][0];
            locy = loc[point][1];
          } 
          L1Teleport.teleport(pc, locx, locy, pc.getMapId(), pc.getHeading(), true);
          pc.sendPackets((ServerBasePacket)new S_PacketBox(176, pc));
          break;
      } 
      if (pc != null && pc.get_mazu_time() != 0L && pc.is_mazu()) {
        Calendar cal = Calendar.getInstance();
        long h_time = cal.getTimeInMillis() / 1000L;
        if (h_time - pc.get_mazu_time() >= 1800L) {
          pc.set_mazu_time(0L);
          pc.set_mazu(false);
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
